package com.example.exam10.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import com.example.exam10.AppDatabase;
import com.example.exam10.R;
import com.example.exam10.dao.PersonDao;
import com.example.exam10.entity.Person;

public class PersonActivity extends AppCompatActivity {
    private static final String TAG = "PersonActivity.class";
    EditText etName, etPhone;
    Button btnSave;
    PersonDao personDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_person);
        personDao = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "data.db")
                .allowMainThreadQueries()
                .build().getPersonDao();
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(v -> {
            Person person = new Person();
            person.setName(etName.getText().toString());
            person.setPhone(etPhone.getText().toString());
            personDao.insertPerson(person);
            Intent intent = new Intent(PersonActivity.this, exam10.class);
            startActivity(intent);
        });
    }
}
